﻿using System;
using System.IO;
using System.Linq;

namespace say
{
    class Program
    {
        static void Main(string[] args)
        {
            String e = " ";
            System.Speech.Synthesis.SpeechSynthesizer ret = new System.Speech.Synthesis.SpeechSynthesizer();
            bool filed; filed = false;
            String a = " ";
            ret.Rate = -2;
            if (args.Count() >= 1)//input syntax is say outfile infile, there is no way to have the infile without the outfile
            {
                ret.SetOutputToWaveFile(args[0]);
                if (args.Count() >= 2)//a bit less flexible than I'd like but it'll have to do for now
                {
                    FileStream q; q = File.Open(args[1], FileMode.Open);
                    int filelength = (int)q.Seek(0, SeekOrigin.End);
                    byte[] u = new byte[filelength];
                    q.Seek(0, SeekOrigin.Begin);
                    q.Read(u, 0, filelength);
                    System.Text.ASCIIEncoding wert = new System.Text.ASCIIEncoding();
                    e = wert.GetString(u);
                    e = null;
                    filed = true;
                }
            }
            if (filed == false)//prevents waste
            {
                //not the best way to do it, but it works consistantly
                while (a != "quit")
                {
                    a = System.Console.ReadLine();
                    if (a != "quit")//prevents it from reading out quit
                        ret.Speak(a);//say through either speakers or outfile depending on what it is set to
                }
            }
            else
            {
                ret.Speak(e);
            }
            ret = null;
        }
    }
}